package com.easyjop.Controller;

import com.easyjop.annotation.GlobalInterceptor;
import com.easyjop.annotation.VerifyParam;
import com.easyjop.entity.enums.PostStatusEnum;
import com.easyjop.entity.enums.RequestFrequencyTypeEnum;
import com.easyjop.entity.enums.ResponseCodeEnum;
import com.easyjop.entity.po.AppCarousel;
import com.easyjop.entity.po.AppDevice;
import com.easyjop.entity.po.ExamQuestion;
import com.easyjop.entity.po.ExamQuestionItem;
import com.easyjop.entity.query.AppCarouselQuery;
import com.easyjop.entity.query.ExamQuestionItemQuery;
import com.easyjop.entity.vo.ResponseVO;
import com.easyjop.exception.BusinessException;
import com.easyjop.service.AppCarouselService;
import com.easyjop.service.AppDeviceService;
import com.easyjop.service.CategoryService;
import com.easyjop.service.ExamQuestionService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController("indexController")
@RequestMapping("/index")
public class IndexController extends ABaseController {

    @Resource
    private CategoryService categoryService;
    @Resource
    private AppCarouselService appCarouselService;
    @Resource
    private ExamQuestionService examQuestionService;
    @Resource
    private AppDeviceService appDeviceService;
    @RequestMapping("/loadAllCategory")
    @GlobalInterceptor
    public ResponseVO loadAllCategory(@VerifyParam(required = true) Integer type) {
        return getSuccessResponseVO(categoryService.loadAllCategoryByType(type));
    }
    @RequestMapping("/loadCarousel")
    @GlobalInterceptor
    public ResponseVO loadCarousel() {
        AppCarouselQuery query = new AppCarouselQuery();
        query.setOrderBy("sort asc");
        List<AppCarousel> carouselList = appCarouselService.findListByParam(query);
        return getSuccessResponseVO(carouselList);
    }
    @RequestMapping("/getExamQuestionById")
    @GlobalInterceptor
    public ResponseVO getExamQuestionById(@VerifyParam(required = true) Integer questionId) {
        ExamQuestion examQuestionByQuestionId = examQuestionService.getExamQuestionAndItmByQuestionId(questionId);
        return getSuccessResponseVO(examQuestionByQuestionId);
    }
    @RequestMapping("/report")
    @GlobalInterceptor(checkParams = false, frequencyType = RequestFrequencyTypeEnum.DAY, reqeustFrequencyThreshold = 10)
    public ResponseVO report(HttpServletRequest request,
                             @VerifyParam(required = true, max = 32) String deviceId,
                             @VerifyParam(max = 30) String deviceBrand) {
        AppDevice appDevice = new AppDevice();
        appDevice.setDeviceId(deviceId);
        appDevice.setDeviceBrand(deviceBrand);
        appDevice.setIp(getIpAddr(request));
        appDeviceService.reportData(appDevice);
        return getSuccessResponseVO(null);
    }
}
